package com.sale.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.mapper.Mapper;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.system.api.domain.dto.DeviceApiRecordDto;
import com.sale.system.api.domain.vo.ReturnApiDeviceStatusVo;
import com.sale.system.domain.dto.DeviceOperationDto;
import com.sale.system.domain.dto.DeviceRecordDto;
import com.sale.system.domain.dto.GetDeviceInfoDto;
import com.sale.system.domain.vo.DeviceRecordVo;
import com.sale.system.domain.vo.ReturnDeviceStatusVo;
import com.sale.system.service.DeviceRecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 设备台账控制层
 *
 * @author: CM
 * @date: 2023/2/13  11:23
 */
@RestController
@RequestMapping("/device")
public class DeviceRecordController extends BaseController {

    @Resource
    private DeviceRecordService deviceRecordService;

    /**
     * 设备台账查询列表
     */
    @SaCheckPermission("device:equipment:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceRecordDto> list(DeviceRecordVo deviceRecordVo, PageQuery pageQuery) {
        return deviceRecordService.getList(deviceRecordVo, pageQuery);
    }

    /**
     * 新增设备台账
     */
    @SaCheckPermission("system:shopAdministration:equipment:add")
    @Log(title = "新增设备台账  ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody DeviceRecordVo deviceRecordVo) throws Exception {
        return toAjax(deviceRecordService.deviceAdd(deviceRecordVo));
    }

    /**
     * 修改设备台账
     */
    @SaCheckPermission("system:shopAdministration:equipment:edit")
    @Log(title = "修改设备台账", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody @Validated DeviceRecordVo deviceRecordVo) {
        return toAjax(deviceRecordService.deviceEdit(deviceRecordVo));
    }

    /**
     * 查看设备台账明细
     */
    @SaCheckPermission("system:shopAdministration:equipment:query")
    @GetMapping("/view")
    public R<DeviceRecordDto> view(long id) {
        return R.ok(deviceRecordService.view(id));
    }

    /**
     * 删除设备台账信息
     */
    @SaCheckPermission("system:shopAdministration:equipment:remove")
    @Log(title = "删除配置变量", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove(@PathVariable("ids") String ids) {
        return toAjax(deviceRecordService.remove(ids));
    }

    /**
     * 通过线别编码查看列表信息
     */
    @GetMapping("/getLine")
    public R<DeviceRecordDto> getLine(String lineCode) {
        return R.ok(deviceRecordService.getLine(lineCode));
    }

    /**
     * 通过设备编码查出设备名称,设备类型,设备属性
     */
    @GetMapping("/getDeviceInfo")
    public R<GetDeviceInfoDto> getDeviceInfo(String deviceCode) {
        return R.ok(deviceRecordService.getDeviceInfo(deviceCode));
    }

    /**
     * 查询设备运行情况(feign)
     */
    @GetMapping("/getDeviceCondition")
    public R<List<DeviceOperationDto>> getDeviceCondition(String lineCode) {
        List<DeviceOperationDto> deviceCondition = deviceRecordService.getDeviceCondition(lineCode);
        return R.ok(deviceCondition);
    }

    /**
     * 设备状态回传（dubbo）
     */
    @PostMapping("/returnDeviceStatus")
    public R<Void> returnDeviceStatus(@RequestBody ReturnApiDeviceStatusVo returnDeviceStatusVo) {
        return toAjax(deviceRecordService.returnDeviceStatus(returnDeviceStatusVo));
    }

    /**
     * 根据id获取工艺卡信息
     */
    @GetMapping("/getDeviceByIds")
    public List<DeviceApiRecordDto> getDeviceByIds(String ids) {
        return deviceRecordService.getDeviceByIds(ids);
    }

    /**
     * 通过设备编码获取设备信息
     */
    @GetMapping("/selectEquipmentById")
    public DeviceApiRecordDto selectEquipmentById(String code) {
        return deviceRecordService.selectEquipmentById(code);
    }

    /**
     * 根据设备id获取信息
     */
    @GetMapping("/getDeviceById")
    public DeviceApiRecordDto getDeviceById(Long id) {
        return deviceRecordService.getDeviceById(id);
    }

    /**
     * 设备map列表
     */
    @GetMapping("/getMapData")
    public Map<String,DeviceApiRecordDto> getMapData(){
        return deviceRecordService.getMapData();
    }

}
